package com.ffcc.fitness.comment.service.impl;

import com.ffcc.fitness.comment.mapper.CommentMapper;
import com.ffcc.fitness.comment.pojo.dto.CommentAddParam;
import com.ffcc.fitness.comment.pojo.dto.CommentUpdateParam;
import com.ffcc.fitness.comment.pojo.entity.Comment;
import com.ffcc.fitness.comment.service.CommentService;
import com.ffcc.fitness.card.pojo.vo.CommentVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论服务实现类（简化版）
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {
    
    private final CommentMapper commentMapper;
    
    @Override
    public void addComment(CommentAddParam param) {
        log.info("添加评论，参数：{}", param);
        
        // 参数校验
        if (param.getTargetId() == null || param.getTargetType() == null 
            || param.getUserId() == null || param.getContent() == null) {
            throw new IllegalArgumentException("评论参数不能为空");
        }
        
        if (param.getContent().trim().isEmpty()) {
            throw new IllegalArgumentException("评论内容不能为空");
        }
        
        if (param.getContent().length() > 500) {
            throw new IllegalArgumentException("评论内容不能超过500字符");
        }
        
        // 目标类型验证：1为首页器械卡片，2为课程，3为交易商品器械卡片
        if (param.getTargetType() < 1 || param.getTargetType() > 3) {
            throw new IllegalArgumentException("无效的目标类型");
        }
        
        Comment comment = new Comment();
        comment.setUserId(param.getUserId());
        comment.setTargetId(param.getTargetId());
        comment.setTargetType(param.getTargetType());
        comment.setContent(param.getContent().trim());
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        
        int result = commentMapper.insert(comment);
        if (result != 1) {
            throw new RuntimeException("添加评论失败");
        }
        
        log.info("成功添加评论，用户ID：{}，目标ID：{}，目标类型：{}", 
                param.getUserId(), param.getTargetId(), param.getTargetType());
    }
    
    @Override
    public List<CommentVO> getCommentsByTarget(Long targetId, Integer targetType) {
        log.info("查询目标评论列表，目标ID：{}，目标类型：{}", targetId, targetType);
        
        if (targetId == null || targetType == null) {
            throw new IllegalArgumentException("目标ID和目标类型不能为空");
        }
        
        if (targetType < 1 || targetType > 3) {
            throw new IllegalArgumentException("无效的目标类型");
        }
        
        List<CommentVO> comments = commentMapper.selectCommentsByTarget(targetId, targetType);
        log.info("查询到{}条评论", comments.size());
        
        return comments;
    }
    
    @Override
    public Long getCommentCount(Long targetId, Integer targetType) {
        log.info("查询目标评论数量，目标ID：{}，目标类型：{}", targetId, targetType);
        
        if (targetId == null || targetType == null) {
            throw new IllegalArgumentException("目标ID和目标类型不能为空");
        }
        
        if (targetType < 1 || targetType > 3) {
            throw new IllegalArgumentException("无效的目标类型");
        }
        
        Long count = commentMapper.getCommentCount(targetId, targetType);
        log.info("目标评论数量：{}", count);
        
        return count == null ? 0L : count;
    }
    
    @Override
    public void updateComment(CommentUpdateParam param) {
        log.info("更新评论，参数：{}", param);
        
        if (param.getCommentId() == null || param.getUserId() == null 
            || param.getContent() == null) {
            throw new IllegalArgumentException("评论更新参数不能为空");
        }
        
        if (param.getContent().trim().isEmpty()) {
            throw new IllegalArgumentException("评论内容不能为空");
        }
        
        if (param.getContent().length() > 500) {
            throw new IllegalArgumentException("评论内容不能超过500字符");
        }
        
        // 先查询评论是否存在
        Comment comment = commentMapper.selectById(param.getCommentId());
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }
        
        // 验证用户权限（只能修改自己的评论）
        if (!comment.getUserId().equals(param.getUserId())) {
            throw new RuntimeException("无权限修改此评论");
        }
        
        int result = commentMapper.updateContent(param.getCommentId(), 
                                               param.getUserId(), 
                                               param.getContent().trim());
        if (result != 1) {
            throw new RuntimeException("更新评论失败");
        }
        
        log.info("成功更新评论，评论ID：{}，用户ID：{}", param.getCommentId(), param.getUserId());
    }
    
    @Override
    public void deleteComment(Long commentId, Long userId) {
        log.info("删除评论，评论ID：{}，用户ID：{}", commentId, userId);
        
        if (commentId == null || userId == null) {
            throw new IllegalArgumentException("评论ID和用户ID不能为空");
        }
        
        // 先查询评论是否存在
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }
        
        // 验证用户权限（只能删除自己的评论）
        if (!comment.getUserId().equals(userId)) {
            throw new RuntimeException("无权限删除此评论");
        }
        
        int result = commentMapper.deleteByIdAndUserId(commentId, userId);
        if (result != 1) {
            throw new RuntimeException("删除评论失败");
        }
        
        log.info("成功删除评论，评论ID：{}，用户ID：{}", commentId, userId);
    }
}